INSTALLED_LK$(LK_MODE)_TARGET := $(PRODUCT_OUT)/lk$(call to-lower,$(LK_MODE)).img
INSTALLED_LOGO$(LK_MODE)_TARGET := $(PRODUCT_OUT)/logo$(call to-lower,$(LK_MODE)).bin
TARGET_LK$(LK_MODE)_OUT := $(TARGET_OUT_INTERMEDIATES)/LK$(LK_MODE)_OBJ
TARGET_LK$(LK_MODE)_ROOT_OUT := $(if $(filter /% ~%,$(TARGET_LK$(LK_MODE)_OUT)),,$(LK_ROOT_DIR)/)$(TARGET_LK$(LK_MODE)_OUT)
BUILT_LK$(LK_MODE)_TARGET := $(TARGET_LK$(LK_MODE)_OUT)/build-$(LK_PROJECT)/lk.img

ALL_BUILT_LK_TARGET := $(ALL_BUILT_LK_TARGET) $(BUILT_LK$(LK_MODE)_TARGET)

LK_MAKE_OPTION := $(if $(SHOW_COMMANDS),NOECHO=) $(if $(LK_CROSS_COMPILE),TOOLCHAIN_PREFIX=$(LK_CROSS_COMPILE)) BOOTLOADER_OUT=$(TARGET_LK$(LK_MODE)_ROOT_OUT) ROOTDIR=$(LK_ROOT_DIR)
LK_MAKE_OPTION += LCM_WIDTH=$(LCM_WIDTH) LCM_HEIGHT=$(LCM_HEIGHT)
LK_MAKE_OPTION += COMPILER=$(LK_ROOT_DIR)/$(SOONG_CLANG)

ifeq ($(LK_MODE),_DEF_UNLOCK)
LK_MAKE_OPTION += MTK_BUILD_DEFAULT_UNLOCK=yes
endif

ifeq ($(LK_MODE),_ENHANCE_MENU)
LK_MAKE_OPTION += MTK_BUILD_ENHANCE_MENU=yes
endif

ifeq ($(LK_MODE),_DEEP_GPT_UPDATE)
LK_MAKE_OPTION += MTK_GPT_UPDATE_SUPPORT=yes
LK_MAKE_OPTION += MTK_BUILD_DEFAULT_UNLOCK=yes
LK_MAKE_OPTION += MTK_SECURITY_SW_SUPPORT=no
endif

ifeq ($(LK_MODE),_FES)
LK_MAKE_OPTION += MTK_SECURITY_SW_SUPPORT=no
LK_MAKE_OPTION += CFG_DTB_EARLY_LOADER_SUPPORT=no
LK_MAKE_OPTION += PLATFORM_FASTBOOT_EMPTY_STORAGE=yes
LK_MAKE_OPTION += APPSBOOTHDR_FILES=
endif

.KATI_RESTAT: $(BUILT_LK$(LK_MODE)_TARGET)
$(BUILT_LK$(LK_MODE)_TARGET): PRIVATE_MAKE_OPTION := $(LK_MAKE_OPTION)
$(BUILT_LK$(LK_MODE)_TARGET): $(LK_MAKE_DEPENDENCIES)
	$(hide) mkdir -p $(dir $@)
	$(PREBUILT_MAKE_PREFIX)$(MAKE) -C $(LK_DIR) $(PRIVATE_MAKE_OPTION) $(LK_PROJECT)

$(INSTALLED_LK$(LK_MODE)_TARGET): $(BUILT_LK$(LK_MODE)_TARGET) $(MTK_LK_DTB_TARGET)
	$(hide) mkdir -p $(dir $@)
	$(hide) cat $< $(MTK_LK_DTB_TARGET) > $@

$(INSTALLED_LOGO$(LK_MODE)_TARGET): $(BUILT_LK$(LK_MODE)_TARGET)
	$(hide) mkdir -p $(dir $@)
	$(hide) cp -f $(dir $<)logo.bin $@

.PHONY: lk clean-lk clean-lk$(LK_MODE)

clean-lk: clean-lk$(LK_MODE)
clean-lk$(LK_MODE): PRIVATE_MODE := $(LK_MODE)
clean-lk$(LK_MODE):
	$(hide) rm -rf $(INSTALLED_LK$(PRIVATE_MODE)_TARGET) $(INSTALLED_LOGO$(PRIVATE_MODE)_TARGET) $(TARGET_LK$(PRIVATE_MODE)_OUT)

ifneq ($(LK_MODE),_FES)
droidcore lk: $(INSTALLED_LK$(LK_MODE)_TARGET) $(INSTALLED_LOGO$(LK_MODE)_TARGET)
endif
